package cn.ecut.dzy.sync;

/**
 * @program: juc
 * @description:
 * @author: DingZhenYun
 * @create: 2021-10-19 13:29
 **/

public class DeadLockTest {

    static class LockRunner {
        public void lock(Object owner, Object target) throws InterruptedException {
            synchronized (owner) {
                Thread.sleep(1000);
                synchronized (target) {
                    System.out.println("ok");
                }
            }
        }
    }



    public static void main(String[] args) throws InterruptedException {
        Object lockA = new Object();
        Object lockB = new Object();
        LockRunner lockRunner = new LockRunner();
        DeadLockTest deadLockTest=new DeadLockTest();
        /**
         * 不会出现死锁，现在是主线程在执行。程序的顺序执行，第七行开没有开启多线程，没有抢占资源
         */
        lockRunner.lock(lockA, lockB);
        //开启异步线程
        new Thread(() -> {
            try {
                lockRunner.lock(lockB, lockA);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        System.out.println("------");
       // lockRunner.lock(lockA, lockB);

    }
}
